From 55f17a3a03ff9faa574549f480580296c46f81e8 Mon Sep 17 00:00:00 2001 From: alexmot Date: Mon, 27 Oct 2003 15:06:20 +0000 Subject: [PATCH] Add WHITESPACE delimiter to xcsv. --- gpsbabel/csv_util.c | 30 +++++++++++++++++++++++------- gpsbabel/internal_styles.c | 2 +- gpsbabel/style/README.style | 14 ++++++++++++++ gpsbabel/style/gpsdrive.style | 2 +- gpsbabel/xcsv.c | 1 + 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/gpsbabel/csv_util.c b/gpsbabel/csv_util.c index 45e11d2e1..c5786dfaf 100644 --- a/gpsbabel/csv_util.c +++ b/gpsbabel/csv_util.c @@ -30,6 +30,7 @@ #define LAT_DIR(a) a < 0.0 ? 'S' : 'N' #define LON_DIR(a) a < 0.0 ? 'W' : 'E' #define NONULL(a) a ? a : "" +#define ISWHITESPACE(a) ((a == ' ') || (a == '\t')) /* convert excel time (days since 1900) to time_t and back again */ #define EXCEL_TO_TIMET(a) ((a - 25569.0) * 86400.0) @@ -165,11 +166,15 @@ csv_lineparse(const char *stringstart, const char *delimited_by, size_t dlen = 0, elen = 0; int enclosedepth = 0; short int dfound; + short int hyper_whitespace_delimiter = 0; if (tmp) { xfree(tmp); tmp = NULL; } + + if (strcmp(delimited_by, "\\w") == 0) + hyper_whitespace_delimiter = 1; if (!p) { /* first pass thru */ @@ -185,7 +190,7 @@ csv_lineparse(const char *stringstart, const char *delimited_by, sp = p; /* length of delimiters and enclosures */ - if (delimited_by) + if ((delimited_by) && (!hyper_whitespace_delimiter)) dlen = strlen(delimited_by); if (enclosed_in) elen = strlen(enclosed_in); @@ -200,13 +205,19 @@ csv_lineparse(const char *stringstart, const char *delimited_by, enclosedepth++; } - if ((!enclosedepth) && (strncmp(p, delimited_by, dlen) == 0)) { - dfound = 1; - } else { - p++; + if (!enclosedepth) { + if ((dlen) && (strncmp(p, delimited_by, dlen) == 0)) { + dfound = 1; + } else if ((hyper_whitespace_delimiter) && (ISWHITESPACE(*p))) { + dfound = 1; + while (ISWHITESPACE(*p)) + p++; + } else { + p++; + } } } - + /* allocate enough space for this data field */ tmp = xcalloc((p - sp) + 1, sizeof(char)); @@ -586,10 +597,15 @@ xcsv_waypt_pr(const waypoint *wpt) char *shortname = NULL; char *description = NULL; char * anyname = NULL; + char * write_delimiter; int i; field_map_t *fmp; queue *elem, *tmp; + if (strcmp(xcsv_file.field_delimiter, "\\w") == 0) + write_delimiter = " "; + else + write_delimiter = xcsv_file.field_delimiter; if ((! wpt->shortname) || (global_opts.synthesize_shortnames)) { if (wpt->description) { @@ -627,7 +643,7 @@ xcsv_waypt_pr(const waypoint *wpt) fmp = (field_map_t *) elem; if (i != 0) - fprintf (xcsv_file.xcsvfp, xcsv_file.field_delimiter); + fprintf (xcsv_file.xcsvfp, write_delimiter); i++; diff --git a/gpsbabel/internal_styles.c b/gpsbabel/internal_styles.c index 29b5b11eb..38e88b66f 100644 --- a/gpsbabel/internal_styles.c +++ b/gpsbabel/internal_styles.c @@ -187,7 +187,7 @@ static char gpsdrive[] = "# FILE LAYOUT DEFINITIIONS:\n" "#\n" -"FIELD_DELIMITER SPACE\n" +"FIELD_DELIMITER WHITESPACE\n" "RECORD_DELIMITER NEWLINE\n" "BADCHARS ,'\"\n" diff --git a/gpsbabel/style/README.style b/gpsbabel/style/README.style index 740f124d2..5f0152818 100644 --- a/gpsbabel/style/README.style +++ b/gpsbabel/style/README.style @@ -32,6 +32,19 @@ CRNEWLINE \r\n TAB \t SPACE HASH # +WHITESPACE *** SEE WHITESPACE NOTES BELOW *** + +WHITESPACE: +The WHITESPACE constant has special properties. When reading data, +WHITESPACE refers to sequential runs of SPACES and/or TABS. When +writing data, WHITESPACE is always a single SPACE. + +For example, the following line: +SOME_NAME 30.1208 -91.1365 SOME OTHER NAME + +Parses into the following data fields: +SOME_NAME,30.1208,-91.1365,SOME,OTHER,NAME + COMMENTS: Anything after a hash (#) on a line is not parsed. For example: @@ -40,6 +53,7 @@ Anything after a hash (#) on a line is not parsed. For example: FIELD LAT_DECIMAL, "", "%f" # ONLY THIS SENTENCE IS A COMMENT. + GLOBAL PROPERTIES OF THE FILE: -------------------------------- There are a few available directives to describe general traits of the diff --git a/gpsbabel/style/gpsdrive.style b/gpsbabel/style/gpsdrive.style index 96cbda45f..dfe80ccaa 100644 --- a/gpsbabel/style/gpsdrive.style +++ b/gpsbabel/style/gpsdrive.style @@ -11,7 +11,7 @@ DESCRIPTION GpsDrive Format # FILE LAYOUT DEFINITIIONS: # -FIELD_DELIMITER SPACE +FIELD_DELIMITER WHITESPACE RECORD_DELIMITER NEWLINE BADCHARS ,'" diff --git a/gpsbabel/xcsv.c b/gpsbabel/xcsv.c index b2e09ceca..9d16f4c14 100644 --- a/gpsbabel/xcsv.c +++ b/gpsbabel/xcsv.c @@ -72,6 +72,7 @@ char_map_t xcsv_char_table[] = { { "TAB", "\t" }, { "SPACE", " " }, { "HASH", "#" }, + { "WHITESPACE", "\\w" }, { NULL, NULL } }; -- 2.30.2